package com.hydong.novatest.activity

import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import com.hydong.novatest.R
import com.hydong.novatest.Utils.QDSkinManager
import com.hydong.novatest.base.BaseActivity
import com.qmuiteam.qmui.skin.QMUISkinManager
import com.qmuiteam.qmui.util.QMUIPackageHelper
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView
import kotlinx.android.synthetic.main.activity_about.*
import java.lang.String
import java.text.SimpleDateFormat
import java.util.*

class AboutActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val skinManager = QMUISkinManager.defaultInstance(this)
        setSkinManager(skinManager)
        mOnSkinChangeListener.onSkinChange(skinManager, -1, skinManager.currentSkin)

        val root: View = LayoutInflater.from(this).inflate(R.layout.activity_about, null)
        setContentView(root)

        initView()
    }

    private fun initView() {
        version?.text = QMUIPackageHelper.getAppVersion(this)
        val dateFormat = SimpleDateFormat("yyyy", Locale.CHINA)
        val currentYear = dateFormat.format(Date())
        copyright?.text = String.format(resources.getString(R.string.about_copyright), currentYear)

        topbar?.setTitle(getString(R.string.about_title))
        topbar?.addLeftBackImageButton()?.setOnClickListener {
            popBackStack()
        }


        QMUIGroupListView.newSection(this)
            .addItemView(about_list?.createItemView("访问官网")) {
//                val url = "https://qmuiteam.com/android"
//                val bundle = Bundle()
//                bundle.putString(EXTRA_URL, url)
//                bundle.putString(EXTRA_TITLE, resources.getString(R.string.about_item_homepage))
//                val fragment: QMUIFragment = QDWebExplorerFragment()
//                fragment.arguments = bundle
//                startFragment(fragment)
            }
            .addItemView(about_list?.createItemView("GitHub")) {
//                val url = "https://github.com/Tencent/QMUI_Android"
//                val bundle = Bundle()
//                bundle.putString(EXTRA_URL, url)
//                bundle.putString(EXTRA_TITLE, resources.getString(R.string.about_item_github))
//                val fragment: QMUIFragment = QDWebExplorerFragment()
//                fragment.arguments = bundle
//                startFragment(fragment)
            }
            .addTo(about_list)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
    }

    override fun onStart() {
        super.onStart()
        skinManager?.addSkinChangeListener(mOnSkinChangeListener)
    }

    override fun onStop() {
        super.onStop()
        skinManager?.removeSkinChangeListener(mOnSkinChangeListener)
    }

    private val mOnSkinChangeListener = QMUISkinManager.OnSkinChangeListener { skinManager, oldSkin, newSkin ->
        if (newSkin == QDSkinManager.SKIN_WHITE) {
            QMUIStatusBarHelper.setStatusBarLightMode(this)
        } else {
            QMUIStatusBarHelper.setStatusBarDarkMode(this)
        }
    }

}